package cn.stylefeng.guns.modular.jhb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.BusinessException;
import cn.stylefeng.guns.modular.jhb.entity.Jhb;
import cn.stylefeng.guns.modular.jhb.enums.JhbExceptionEnum;
import cn.stylefeng.guns.modular.jhb.mapper.JhbMapper;
import cn.stylefeng.guns.modular.jhb.pojo.request.JhbRequest;
import cn.stylefeng.guns.modular.jhb.service.JhbService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 班次计划表业务实现层
 *
 * @author zhu
 * @date 2021/10/06 11:17
 */
@Service
public class JhbServiceImpl extends ServiceImpl<JhbMapper, Jhb> implements JhbService {

	@Override
    public void add(JhbRequest jhbRequest) {
        Jhb jhb = new Jhb();
        BeanUtil.copyProperties(jhbRequest, jhb);
        this.save(jhb);
    }

    @Override
    public void del(JhbRequest jhbRequest) {
        Jhb jhb = this.queryJhb(jhbRequest);
        this.removeById(jhb.getId());
    }

    @Override
    public void edit(JhbRequest jhbRequest) {
        Jhb jhb = this.queryJhb(jhbRequest);
        BeanUtil.copyProperties(jhbRequest, jhb);
        this.updateById(jhb);
    }

    @Override
    public Jhb detail(JhbRequest jhbRequest) {
        return this.queryJhb(jhbRequest);
    }

    @Override
    public PageResult<Jhb> findPage(JhbRequest jhbRequest) {
        LambdaQueryWrapper<Jhb> wrapper = createWrapper(jhbRequest);
        Page<Jhb> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<Jhb> findList(JhbRequest jhbRequest) {
        LambdaQueryWrapper<Jhb> wrapper = this.createWrapper(jhbRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author zhu
     * @date 2021/10/06 11:17
     */
    private Jhb queryJhb(JhbRequest jhbRequest) {
        Jhb jhb = this.getById(jhbRequest.getId());
        if (ObjectUtil.isEmpty(jhb)) {
            throw new BusinessException(JhbExceptionEnum.JHB_NOT_EXISTED);
        }
        return jhb;
    }

    /**
     * 创建查询wrapper
     *
     * @author zhu
     * @date 2021/10/06 11:17
     */
    private LambdaQueryWrapper<Jhb> createWrapper(JhbRequest jhbRequest) {
        LambdaQueryWrapper<Jhb> queryWrapper = new LambdaQueryWrapper<>();

        Long id = jhbRequest.getId();
        String bcbh = jhbRequest.getBcbh();
        String fcsj = jhbRequest.getFcsj();
        String realTime = jhbRequest.getRealTime();
        Integer tqsj = jhbRequest.getTqsj();
        String bclx = jhbRequest.getBclx();
        String bczt = jhbRequest.getBczt();
        String xlbXlbh = jhbRequest.getXlbXlbh();
        String clbBbkh = jhbRequest.getClbBbkh();
        Boolean autoSubmit = jhbRequest.getAutoSubmit();
        String createTime = jhbRequest.getCreateTime();
        Long createUser = jhbRequest.getCreateUser();
        String updateTime = jhbRequest.getUpdateTime();
        Long updateUser = jhbRequest.getUpdateUser();

        queryWrapper.eq(ObjectUtil.isNotNull(id), Jhb::getId, id);
        queryWrapper.like(ObjectUtil.isNotEmpty(bcbh), Jhb::getBcbh, bcbh);
        queryWrapper.eq(ObjectUtil.isNotNull(fcsj), Jhb::getFcsj, fcsj);
        queryWrapper.eq(ObjectUtil.isNotNull(realTime), Jhb::getRealTime, realTime);
        queryWrapper.eq(ObjectUtil.isNotNull(tqsj), Jhb::getTqsj, tqsj);
        queryWrapper.like(ObjectUtil.isNotEmpty(bclx), Jhb::getBclx, bclx);
        queryWrapper.like(ObjectUtil.isNotEmpty(bczt), Jhb::getBczt, bczt);
        queryWrapper.like(ObjectUtil.isNotEmpty(xlbXlbh), Jhb::getXlbXlbh, xlbXlbh);
        queryWrapper.like(ObjectUtil.isNotEmpty(clbBbkh), Jhb::getClbBbkh, clbBbkh);
        queryWrapper.eq(ObjectUtil.isNotNull(autoSubmit), Jhb::getAutoSubmit, autoSubmit);
        queryWrapper.eq(ObjectUtil.isNotNull(createTime), Jhb::getCreateTime, createTime);
        queryWrapper.eq(ObjectUtil.isNotNull(createUser), Jhb::getCreateUser, createUser);
        queryWrapper.eq(ObjectUtil.isNotNull(updateTime), Jhb::getUpdateTime, updateTime);
        queryWrapper.eq(ObjectUtil.isNotNull(updateUser), Jhb::getUpdateUser, updateUser);

        return queryWrapper;
    }

}